package com.kucai.yifaqapp.presenter.fragment;

import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResGameDetails;
import com.kucai.yifaqapp.model.network.entity.res.ResMoney;
import com.kucai.yifaqapp.model.network.entity.res.ResSetbet;
import com.kucai.yifaqapp.model.network.entity.res.Resjsliuhecai;
import com.kucai.yifaqapp.model.network.entity.res.Ressanfenhecai;
import com.kucai.yifaqapp.model.network.entity.res.Resxgliuhecai;
import com.kucai.yifaqapp.model.network.entity.res.Resxyliuhecai;
import com.kucai.yifaqapp.ui.fragment.gameFragment.LHCSFFragment;
import com.kucai.yifaqapp.ui.fragment.gameFragment.LHCYFFragment;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/10.
 */

public class LHCSFFragmentPresenter extends BaseFragmentPresenter<LHCSFFragment> {


    public LHCSFFragmentPresenter(LHCSFFragment mFragment) {
        super(mFragment);
    }

    //获取游戏详情
    public void getgamesdetail(String gameid) {
        HttpJsonUtils.getHttpApiInstance()
                .getgamesdetail(HttpAdress.getuseAdress() + "api/m/lottery/getNextIssue.do", gameid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResGameDetails>() {
                    @Override
                    protected void onCustomNext(ResGameDetails resGameDetails) {
                        if (null != resGameDetails.data && resGameDetails.code == 0) {
                            if (("三分六合彩").equals(resGameDetails.data.game_name)) {
                                mFragment.liuhecai(resGameDetails);
                            } else {
                                mFragment.liuhecaifail();
                            }

                        } else {
                            mFragment.liuhecaifail();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        mFragment.liuhecaifail();
                    }
                });
    }//获取游戏详情

    public void getgamesdetail1(String gameid) {
        HttpJsonUtils.getHttpApiInstance()
                .getgamesdetail(HttpAdress.getuseAdress() + "api/m/lottery/getNextIssue.do", gameid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResGameDetails>() {
                    @Override
                    protected void onCustomNext(ResGameDetails resGameDetails) {
                        if (null != resGameDetails.data && resGameDetails.code == 0) {
                            if (("三分六合彩").equals(resGameDetails.data.game_name)) {
                                mFragment.liuhecai1(resGameDetails);
                            } else {
                                mFragment.liuhecaifail();
                            }

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    //下注
    public void Betbet(final String data, String token) {
        HttpJsonUtils.getHttpApiInstance()
                .Betbet(HttpAdress.getuseAdress() + "api/m/lottery/bet.do", data, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResSetbet>(mFragment) {
                    @Override
                    protected void onCustomNext(ResSetbet resOods) {

                        if ("200".equals(resOods.code)) {
                            mFragment.betsuccses();
                        }
//                        else if ("1".equals(resOods.code) || "1".equals(resOods.code)) {
//                            //此期封盘 重置下注
//                            Toast.makeText(mFragment.getContext(), "您下注的期数已封盘，请重新下注", Toast.LENGTH_SHORT).show();
//                            mFragment.Betbetfail();
//                        }
                        else {
                            Toast.makeText(mFragment.getContext(), resOods.msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    //获取余额
    public void getmoney(String token) {
        HttpJsonUtils.getHttpApiInstance()
                .getmoney(HttpAdress.getuseAdress() + "api/m/user/getMoney", token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResMoney>() {
                    @Override
                    protected void onCustomNext(ResMoney resOods) {
                        if (resOods.code == 0) {
                            mFragment.moneychange(resOods);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    public void getsanfenliuhecaiddes(final String gameid, String token) {
        HttpJsonUtils.getHttpApiInstance()
                .getsanfenliuhecaiddes(HttpAdress.getuseAdress() + "api/m/pc/bet/getGameOdds", gameid, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Ressanfenhecai>(mFragment) {
                    @Override
                    protected void onCustomNext(Ressanfenhecai resOods) {

                        if (resOods.code.equals("0")) {
                            mFragment.setxyliuhecaiddes(resOods.data);
                        } else {
                            Toast.makeText(mFragment.getContext(), "赔率获取失败，请重新进入", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

}